<?php
class Chinapay {
	private $_oObj;
	private $_sNo;
	private $_sOrderId;
	private $_sMomey;
	private $_iCuryId = 156;
	private $_sDate;
	private	$_sReturnUrl;
	private	$_sBgUrl;
	
	public function __construct() {
		if (DIRECTORY_SEPARATOR == '\\') {
			$this->_oObj = new COM ( 'CPNPC.NPC' );
		}
		$this->_sDate = date ( 'Ymd' );
	}
	
	public function setPrivateKey($sKeyFile) {
		if (realpath ( $sKeyFile )) {
			$this->_oObj->setMerKeyFile ( realpath ( $sKeyFile ) );
			return true;
		} else {
			return false;
		}
	}
	
	public function setPublicKey($sKeyFile) {
		if (realpath ( $sKeyFile )) {
			$this->_oObj->setPubKeyFile ( realpath ( $sKeyFile ) );
			return true;
		} else {
			return false;
		}
	}
	
	public function setNo($sNo) {
		$this->_sNo = $sNo;
	}
	
	public function setMomey($fMomey) {
		$this->_sMomey = sprintf ( '%012d', $fMomey * 100 );
	}
	
	public function setDate() {
		return $this->_sDate;
	}
	
	public function setOrderId($sOrderId) {
		$this->_sOrderId = $sOrderId;
	}
	
	public function setReturnUrl($sReturnUrl) {
		$this->_sReturnUrl = $sReturnUrl;
	}
	
	public function setBgUrl($sBfUrl) {
		$this->_sBgUrl = $sBfUrl;
	}
	
	public function getFormHtml() {
		$sKey = $this->_oObj->sign ( $this->_sNo, $this->_sOrderId, $this->_sMomey, $this->_iCuryId, $this->_sDate, '0001' );
		$sHtml = '<form action="http://payment.chinapay.com/pay/TransGet" target="_blank" method="post" id="chinapayform">';
		$sHtml .= '<input type=hidden name="MerId" value="' . $this->_sNo . '"/>';
		$sHtml .= '<input type=hidden name="OrdId" value="' . $this->_sOrderId . '"/>';
		$sHtml .= '<input type=hidden name="TransAmt" value="' . $this->_sMomey . '"/>';
		$sHtml .= '<input type=hidden name="CuryId" value="156"/>';
		$sHtml .= '<input type=hidden name="TransDate" value="' . $this->_sDate . '"/>';
		$sHtml .= '<input type=hidden name="TransType" value="0001"/>';
		$sHtml .= '<input type=hidden name="Version" value="20040916"/>';
		$sHtml .= '<input type=hidden name="BgRetUrl" value="'.$this->_sBgUrl.'"/>';
		$sHtml .= '<input type=hidden name="PageRetUrl" value="'.$this->_sReturnUrl.'"/>';
		$sHtml .= '<input type=hidden name="Priv1" value="Memo">';
		$sHtml .= '<input type=hidden name="ChkValue" value="' . $sKey . '">';
		$sHtml .= '</form>';
		return $sHtml;
	}
	
	public function checkData($aPost) {
		return (int)$this->_oObj->check($aPost['merid'], $aPost['orderno'], $aPost['amount'], 156, $aPost['transdate'], $aPost['transtype'], $aPost['status'], $aPost['checkvalue']);
	}
}